package com.sinoeyes.controller.system;

import java.util.List;

import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.constant.UserConstants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysMenuFilter;
import com.sinoeyes.system.service.ISysMenuService;
import com.sinoeyes.system.vo.SysMenuVo;
import com.sinoeyes.system.vo.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单信息
 */
@RestController
@RequestMapping("/system/menu")
@Api(value = "菜单信息 API", tags = {"菜单信息"})
public class SysMenuController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    public Result<List<SysMenuVo>> list(SysMenuFilter menuFilter)
    {
        List<SysMenuVo> menus = menuService.selectMenuList(menuFilter);
        return Result.success(menuService.buildMenuTree(menus));
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    @ApiOperation("根据菜单编号获取详细信息")
    public Result<SysMenuVo> getInfo(@PathVariable Long menuId)
    {
        return Result.success(menuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping("/treeselect")
    @ApiOperation("获取菜单下拉树列表")
    public Result<List<TreeSelect>> treeselect(SysMenuFilter sysMenuFilter)
    {
        List<SysMenuVo> menus = menuService.selectMenuList(sysMenuFilter);
        return Result.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    @ResponseBody
    @ApiOperation("加载对应角色菜单列表树")
    public Result<List<Integer>> roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        return Result.success(menuService.selectMenuListByRoleId(roleId));
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("加载对应角色菜单列表树")
    public Result<Integer> add(@RequestBody @Validated SysMenuVo menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateBy(SecurityUtils.getUsername());
        return Result.success(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody SysMenuVo menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public Result<Integer> remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"菜单已分配,不允许删除");
        }
        return Result.success(menuService.deleteMenuById(menuId));
    }
}